package com.example.vueserver.vueserver.dao.sqlprovider;

import com.example.vueserver.vueserver.bean.SysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

public class SysUserSQLProvider extends SQL {
    private static final String TABLE_NAME = "sys_user";

    public String insertSQL(@Param("sysUser") SysUser sysUser) {
        SQL sql = new SQL();
        sql.INSERT_INTO(TABLE_NAME);
        if (!StringUtils.isEmpty(sysUser.getName())) {
            sql.VALUES("name", "'"+sysUser.getName()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getPassword())) {
            sql.VALUES("password", "'"+sysUser.getName()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getUsername())) {
            sql.VALUES("username", "'"+sysUser.getUsername()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getAddress())) {
            sql.VALUES("address", "'"+sysUser.getAddress()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getDepartment())) {
            sql.VALUES("department", "'"+sysUser.getDepartment()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getEmail())) {
            sql.VALUES("email", "'"+sysUser.getEmail()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getCreateUserId())) {
            sql.VALUES("create_user_id", "'"+sysUser.getCreateUserId()+"'");
        }
        if (!StringUtils.isEmpty(sysUser.getPhoneNo())) {
            sql.VALUES("phone_no", "'"+sysUser.getPhoneNo()+"'");
        }
        System.out.println(sql.toString());
        return sql.toString();
    }

    public String updateSQL(@Param("sysUser") SysUser sysUser){
        SQL sql = new SQL();
        sql.UPDATE(TABLE_NAME);
        if (!StringUtils.isEmpty(sysUser.getName())) {
            sql.SET("name = #{sysUser.name,javaType=String,jdbcType=VARCHAR}");
        }
        if (!StringUtils.isEmpty(sysUser.getPassword())) {
            sql.SET("password=#{sysUser.password,javaType=String,jdbcType=VARCHAR}");
        }
        if (!StringUtils.isEmpty(sysUser.getUsername())) {
            sql.SET("username=#{sysUser.username,javaType=String,jdbcType=VARCHAR}");
        }
        if (!StringUtils.isEmpty(sysUser.getAddress())) {
            sql.SET("address=#{sysUser.address,javaType=String,jdbcType=VARCHAR}");
        }
        if (!StringUtils.isEmpty(sysUser.getDepartment())) {
            sql.SET("department=#{sysUser.department,javaType=Integer,jdbcType=BIGINT}");
        }
        if (!StringUtils.isEmpty(sysUser.getEmail())) {
            sql.SET("email=#{sysUser.email,javaType=String,jdbcType=VARCHAR}");
        }
        if (!StringUtils.isEmpty(sysUser.getCreateUserId())) {
            sql.SET("create_user_id=#{sysUser.createUserId,javaType=Integer,jdbcType=BIGINT}");
        }
        if (!StringUtils.isEmpty(sysUser.getPhoneNo())) {
            sql.SET("phone_no=#{sysUser.phoneNo,javaType=String,jdbcType=VARCHAR}");
        }
        System.out.println(sql.toString());
        sql.WHERE("id=#{sysUser.id,javaType=Integer,jdbcType=BIGINT}");
        return sql.toString();
    }
}
